package org.chatboleh.frame.internal;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.apache.log4j.Logger;
import org.chatboleh.bean.Payee;
import org.chatboleh.exception.EDMException;
import org.chatboleh.util.MessageBox;
import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;

//VS4E -- DO NOT REMOVE THIS LINE!
public class PayeeForm extends JInternalFrame {

	private static final long serialVersionUID = 1L;
	private Logger logger = Logger.getLogger(this.getClass());
	private JTextField txtName;
	private JTextArea txtAddress;
	private JScrollPane jScrollPane0;
	private JTextField txtEmail;
	private JTextField txtPhone;
	private JTextField txtDate;
	private JLabel jLabel4;
	private JLabel jLabel3;
	private JLabel jLabel2;
	private JLabel jLabel1;
	private JLabel jLabel0;
	private JButton btnAction;
	private JButton btnCancel;
	private JTextField txtId;
	private Payee payee;
	
	public PayeeForm() {
		initComponents();
	}
	
	public void clearForm(){
		txtName.setText("");
		txtAddress.setText("");
		txtPhone.setText("");
		txtEmail.setText("");
		txtDate.setText(new Date().toString());
		btnAction.setText("Add");
	}

	private void initComponents() {
		setClosable(true);
		setTitle("Payee");
		setVisible(true);
		setLayout(new GroupLayout());
		add(getTxtName(), new Constraints(new Leading(74, 226, 10, 10), new Leading(17, 12, 12)));
		add(getJScrollPane0(), new Constraints(new Leading(72, 227, 10, 10), new Leading(48, 60, 10, 10)));
		add(getTxtEmail(), new Constraints(new Leading(73, 225, 12, 12), new Leading(117, 10, 10)));
		add(getTxtPhone(), new Constraints(new Leading(73, 225, 12, 12), new Leading(146, 12, 12)));
		add(getTxtDate(), new Constraints(new Leading(73, 225, 12, 12), new Leading(173, 10, 10)));
		add(getJLabel4(), new Constraints(new Leading(12, 12, 12), new Leading(175, 12, 12)));
		add(getJLabel3(), new Constraints(new Leading(12, 12, 12), new Leading(149, 12, 12)));
		add(getJLabel2(), new Constraints(new Leading(12, 12, 12), new Leading(121, 12, 12)));
		add(getJLabel1(), new Constraints(new Leading(12, 12, 12), new Leading(49, 12, 12)));
		add(getJLabel0(), new Constraints(new Leading(12, 12, 12), new Leading(19, 12, 12)));
		add(getBtnAction(), new Constraints(new Leading(69, 10, 10), new Leading(215, 10, 10)));
		add(getBtnCancel(), new Constraints(new Leading(175, 10, 10), new Leading(215, 12, 12)));
		add(getTxtId(), new Constraints(new Leading(235, 12, 12), new Leading(197, 12, 12)));
		setSize(326, 297);
	}

	public JTextField getTxtId() {
		if (txtId == null) {
			txtId = new JTextField();
			txtId.setText("jTextField0");
			txtId.setVisible(false);
		}
		return txtId;
	}

	private JButton getBtnCancel() {
		if (btnCancel == null) {
			btnCancel = new JButton();
			btnCancel.setText("Cancel");
		}
		return btnCancel;
	}

	public JButton getBtnAction() {
		if (btnAction == null) {
			btnAction = new JButton();
			btnAction.setText("Button1");
			btnAction.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent event) {
					btnActionMouseMouseClicked(event);
				}
			});
		}
		return btnAction;
	}

	private JLabel getJLabel0() {
		if (jLabel0 == null) {
			jLabel0 = new JLabel();
			jLabel0.setText("Name:");
		}
		return jLabel0;
	}

	private JLabel getJLabel1() {
		if (jLabel1 == null) {
			jLabel1 = new JLabel();
			jLabel1.setText("Address:");
		}
		return jLabel1;
	}

	private JLabel getJLabel2() {
		if (jLabel2 == null) {
			jLabel2 = new JLabel();
			jLabel2.setText("Email:");
		}
		return jLabel2;
	}

	private JLabel getJLabel3() {
		if (jLabel3 == null) {
			jLabel3 = new JLabel();
			jLabel3.setText("Phone:");
		}
		return jLabel3;
	}

	private JLabel getJLabel4() {
		if (jLabel4 == null) {
			jLabel4 = new JLabel();
			jLabel4.setText("Date:");
		}
		return jLabel4;
	}

	private JTextField getTxtDate() {
		if (txtDate == null) {
			txtDate = new JTextField();
			txtDate.setText("jTextField3");
			txtDate.setEnabled(false);
		}
		return txtDate;
	}

	private JTextField getTxtPhone() {
		if (txtPhone == null) {
			txtPhone = new JTextField();
			txtPhone.setText("jTextField2");
		}
		return txtPhone;
	}

	private JTextField getTxtEmail() {
		if (txtEmail == null) {
			txtEmail = new JTextField();
			txtEmail.setText("jTextField1");
		}
		return txtEmail;
	}

	private JScrollPane getJScrollPane0() {
		if (jScrollPane0 == null) {
			jScrollPane0 = new JScrollPane();
			jScrollPane0.setViewportView(getTxtAddress());
		}
		return jScrollPane0;
	}

	private JTextArea getTxtAddress() {
		if (txtAddress == null) {
			txtAddress = new JTextArea();
			txtAddress.setText("jTextArea0");
		}
		return txtAddress;
	}

	private JTextField getTxtName() {
		if (txtName == null) {
			txtName = new JTextField();
			txtName.setText("jTextField0");
		}
		return txtName;
	}

	private void btnActionMouseMouseClicked(MouseEvent event) {
		if(getBtnAction().getText().equals("Add")){
			// Add new user
			if(validateform()){
				payee = new Payee();
				payee.setName(txtName.getText());
				payee.setAddress(txtAddress.getText());
				payee.setEmail(txtEmail.getText());
				payee.setPhone(txtPhone.getText());
				Calendar cal = Calendar.getInstance();
				payee.setCdate(new Timestamp( cal.getTime().getTime() ));

				try {
					payee.save();
				} catch (EDMException e) {
					logger.error("Fail to create payee.", e);
				}
				MessageBox.info("New payee created.");
				this.dispose();
			}
		}
		if(getBtnAction().getText().equals("Save")){
			if(validateform()){
				if(payee!=null)
					payee = new Payee(Long.parseLong(txtId.getText()));
				payee.setName(txtName.getText());
				payee.setAddress(txtAddress.getText());
				payee.setEmail(txtEmail.getText());
				payee.setPhone(txtPhone.getText());
				try {
					payee.update();
				} catch (EDMException e) {
					logger.error("Fail to update payee.", e);
				}
				MessageBox.info("Payee info saved.");
				this.dispose();
			}
		}
	}

	private boolean validateform() {
		// TODO - To be modified for detail checking.
		if(txtName.getText().length()<1){
			MessageBox.warning("Payee Name cannot be empty.");
			return false;
		}
		if(txtAddress.getText().length()<1){
			MessageBox.warning("Payee Address cannot be empty.");
			return false;
		}
		if(txtEmail.getText().length()<1){
			MessageBox.warning("Payee Email cannot be empty.");
			return false;
		}
		if(txtPhone.getText().length()<1){
			MessageBox.warning("Payee Phone cannot be empty.");
			return false;
		}
		return true;
	}

	public void PrepareEditPayee(Long payeeid) {
		payee = new Payee(payeeid);
		getTxtId().setText(payee.getId()+"");
		getTxtName().setText(payee.getName());
		getTxtAddress().setText(payee.getAddress());
		getTxtEmail().setText(payee.getEmail());
		getTxtPhone().setText(payee.getPhone());
		getTxtDate().setText(payee.getCdate().toString());
	}

}
